/**
 * 
 */
package com.et114.components.jms;

import javax.jms.Destination;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jms.JmsException ;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.util.Assert;
import org.springframework.jms.core.MessageCreator;

import java.util.Iterator;
import java.util.List;

/**
 * @author guanhw
 */
public class JmsQueueManager  {
	protected Log logger = LogFactory.getLog( this.getClass( ) );
	private JmsTemplate jmsTemplate ;
	private Destination senderQueue ;
	private List messageProviders ; 
	
	public JmsTemplate getJmsTemplate ( ) {
		return jmsTemplate;
	}

	public JmsQueueManager ( ) {
		// TODO Auto-generated constructor stub
	}

	public void setJmsTemplate ( JmsTemplate _jmsTemplate ) {
		Assert.notNull( _jmsTemplate , "jmsTemplate is NULL ！" ) ;
		this.jmsTemplate = _jmsTemplate;
	}

	public void setSenderQueue ( Destination senderQueue ) {
		Assert.notNull( senderQueue , "_resolverQueue is NULL！" ) ;
		Assert.isInstanceOf( Destination.class , senderQueue , " "	+ senderQueue.getClass().getName()+ " must implement Destination" );
		this.senderQueue = senderQueue;
	}


	/*
	public List getAfterDos ( ) {
		return afterDos;
	}

	public void setAfterDos ( List _afterDos ) {
		Assert.notEmpty( _afterDos );
		Iterator iter = _afterDos.iterator();
		while ( iter.hasNext() ) {
			Object currentObject = iter.next();
			Assert.isInstanceOf( IAfterSendMessageListener.class, currentObject, " "	+ currentObject.getClass().getName()+ " must implement IAfterSendMessage" );
		}
		this.afterDos = _afterDos;
	}
    */


	public void setMessagePrividers ( List _messageProviders ) {
		Assert.notEmpty( _messageProviders );
		Iterator iter = _messageProviders.iterator();
		while ( iter.hasNext() ) {
			Object currentObject = iter.next();
			Assert.isInstanceOf( IMessageProvider.class, currentObject, " "	+ currentObject.getClass().getName()+ " must implement IMessageProvider" );
		}		
		this.messageProviders = _messageProviders;
	}

	
	public void sendMessage() throws JmsException {
		for ( int i = 0 ; i < messageProviders.size() ; i ++ ) {
			MessageCreator imp = ( MessageCreator ) messageProviders.get( i ) ;
			jmsTemplate.send(  ( Destination )senderQueue , imp ) ; 
		}
	}
	
}
